#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class Stack
{
public:
    Stack(size_t cap =0)
        :_size(0),capacity(cap)
    {
        a = new T[capacity];
    }
    ~Stack()
    {
        delete[] a;
        a = nullptr;
    }
    void pop()
    {
        assert(!empty());
        _size--;
    }
    void checkCapcity()
    {
        if(_size == capacity)
        {
            cout <<"now capacity : "<< capacity<<"  容量满了,扩容...........";
            capacity = capacity == 0 ? 10:capacity*2;//如果最初没有开辟空间,就先给10个,否则2倍扩容
            cout <<"\t扩容后容量为: " << capacity<<endl;
            T* tmp = new T[capacity];
            //数据拷贝
            for(int i = 0;i<_size;i++)
            {
                tmp[i] = a[i];
            }
            //a指向新空间,释放旧空间
            ::swap(a,tmp);
            delete tmp;
        }
    }
    void push(const T& val)
    {
        checkCapcity();
        a[_size++] = val; 
    }
    T& top() const
    {
        return a[_size-1];
    }
    size_t size() const
    {
        return _size;
    }
    bool empty() const
    {
        return _size == 0 ;
    }
private:
    T* a;
    size_t _size = 0;
    size_t capacity = 0;
};
